package day3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class exeUser {
    static List<User1> users=new ArrayList<>();
    private static User1 currentUser=null;
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
        menu();
    }
    public static void menu(){
        while(true){
            if(currentUser==null){
                firstMenu();
            }else{
                SecondMenu();
            }
        }
    }
    public static void firstMenu(){
        while (true){
            System.out.println("欢迎进入一级菜单");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出登录");
            int choice=sc.nextInt();
            switch (choice){
                case 1:register();break;
                case 2:login();break;
                case 3:output();
                    System.exit(0);
            }
        }
    }
    public static void register(){
        List<StringBuilder> phones=new ArrayList<>();
        Random random=new Random();
        for(int i=0;i<9;i++){
            StringBuilder phone=new StringBuilder();
            phone.append("150");
            for(int j=0;j<8;j++){
                phone.append(random.nextInt(10));
            }
            phones.add(phone);
            System.out.println((i+1)+" "+phone);
        }
        System.out.println("请选择你选择的手机号：");
        int number=sc.nextInt();
        //注册
        System.out.println("请输入您要注册的用户名：");
        String username=sc.next();
//        if(users.contains(username)){
//            System.out.println("该用户名已存在，请换一个注册");
//            return;
//        }
        System.out.println("请输入密码：");
        String password=sc.next();
//        System.out.println("请输入电话号码:");
//        String phone=sc.next();
        users.add(new User1(username,password));
        System.out.println("恭喜您注册已成功");
    }
    public static void login(){
        //登录
        System.out.println("请输入用户名:");
        String username=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        String phone=sc.next();
        for(User1 user1:users){
            if(user1.getPassword().equals(password)){
                currentUser = user1;
                System.out.println("\n登录成功！欢迎您，" + user1.getName() + "!");
                SecondMenu();
                break;
            }
        }
        System.out.println("用户名或密码有错误，请重新输入");
    }
    public static void SecondMenu(){
        while(true){
            System.out.println("欢迎进入二级菜单");
            System.out.println("1.展示用户信息");
            System.out.println("2.删除用户信息");
            System.out.println("3.退出登录 到一级菜单");
            int n=sc.nextInt();
            switch (n){
                case 1:show();break;
                case 2:delete();break;
                case 3: currentUser = null;
                    System.out.println("已退出登录");
                    return;
            }
        }
    }
    public static void show(){
        System.out.println("用户名\t密码\t手机号\t");
        for(int i=0;i<users.size();i++){
            User1 u= users.get(i);
            System.out.println((i+1)+"\t"+u.getName()+"\t"
                    +u.getPassword()+"\t");
        }
    }
    public static void delete(){
        show();
        System.out.print("\n请输入要删除的用户编号：");
        int index = sc.nextInt() - 1;
    }
    public static void output(){//序列化
        try(FileOutputStream fos=new FileOutputStream("d://io//user11.xxx");
            ObjectOutputStream obs=new ObjectOutputStream(fos)
        ){
            obs.writeObject(users);
            System.out.println("集合加载成功");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void input(){//反序列化
        try(FileInputStream fis=new FileInputStream("d://io//user11.xxx");
            ObjectInputStream ois=new ObjectInputStream(fis)){
            users = (List<User1>) ois.readObject();
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
class User1 implements Serializable{
    private String name;
    private String password;
    private String phone;

    public User1(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User1(String name, String password, String phone) {
        this.name = name;
        this.password = password;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
